﻿using HandyControl.Controls;
using MiniExcelLibs;
using Ping9719.WpfEx.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media;
using SqlSugar;
using HandyControl.Data;
using System.Windows;

namespace BaseUi.ViewModel
{
    public class DataListViewModel : BindableBase
    {
        private List<MyTestTable> dataList = new List<MyTestTable>();
        /// <summary>
        /// 列表数据
        /// </summary>
        public List<MyTestTable> DataList { get => dataList; set { SetProperty(ref dataList, value); } }

        #region 进度
        private bool isLoad1 = false;
        public bool IsLoad1 { get => isLoad1; set { SetProperty(ref isLoad1, value); } }

        private bool isLoad2 = false;
        public bool IsLoad2 { get => isLoad2; set { SetProperty(ref isLoad2, value); } }
        #endregion

        #region 筛选
        private DateTime? timeGo = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
        /// <summary>
        /// 开始时间（默认只显示今天开始的数据）
        /// </summary>
        public DateTime? TimeGo { get => timeGo; set { SetProperty(ref timeGo, value); } }

        private DateTime? timeTo;
        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? TimeTo { get => timeTo; set { SetProperty(ref timeTo, value); } }

        private string info1;
        public string Info1 { get => info1; set { SetProperty(ref info1, value); } }

        private string info2;
        public string Info2 { get => info2; set { SetProperty(ref info2, value); } }

        #endregion

        #region 页码
        private int maxPageCount;
        /// <summary>
        /// 最大页数
        /// </summary>
        public int MaxPageCount { get => maxPageCount; set { SetProperty(ref maxPageCount, value); } }

        private int pageIndex = 1;
        /// <summary>
        /// 当前页
        /// </summary>
        public int PageIndex { get => pageIndex; set { SetProperty(ref pageIndex, value); } }

        private int dataCountPerPage = 20;
        /// <summary>
        /// 每页的数据量
        /// </summary>
        public int DataCountPerPage { get => dataCountPerPage; set { SetProperty(ref dataCountPerPage, value); } }

        public ICommand PageUpdateCommand { get => new DelegateCommand<FunctionEventArgs<int>>(PageUpdate); }
        /// <summary>
        /// 页码更新
        /// </summary>
        public void PageUpdate(FunctionEventArgs<int> page)
        {
            PageIndex = page.Info;
            UpdateList();
        }
        #endregion

        public ICommand ExportExcelCommand { get => new DelegateCommand(ExportExcel); }
        /// <summary>
        /// 导出为excel
        /// </summary>
        public async void ExportExcel()
        {
            if (IsLoad2)
            {
                Growl.Info("有任务正在进行，请稍等片刻。");
                return;
            }

            Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
            sfd.Filter = ".xlsx文件(*.xlsx)|*.xlsx";
            sfd.FileName = "XXXX记录信息";
            sfd.OverwritePrompt = true;
            if (sfd.ShowDialog() != true)
            {
                IsLoad1 = false;
                return;
            }

            string path = sfd.FileName;

            try
            {
                //1.查询数据库，加入筛选条件，并按照时间倒序排序，并导出指定列
                var dbData = DataDb.db.Queryable<MyTestTable>()
                .WhereIF(TimeGo != null, o => o.Time > TimeGo)
                .WhereIF(TimeTo != null, o => o.Time < TimeTo)
                .WhereIF(!string.IsNullOrWhiteSpace(Info1), o => o.Info1.StartsWith(Info1.Trim()))
                .WhereIF(!string.IsNullOrWhiteSpace(Info2), o => o.Info2.StartsWith(Info2.Trim()))
                .OrderByDescending(o => o.Id)
                .Select(o => new
                {
                    产品信息1 = o.Info1,
                    产品信息2 = o.Info2,
                    状态结果 = o.Status,
                    创建时间 = o.Time.ToString("yyyy-MM-dd HH:mm:ss"),
                });

                //2.导出为Excel，使用内存缓存的方式，防止数据量过大导致内存泄露
                var sqlkv = dbData.ToSql();
                var dataReader = await DataDb.db.Ado.GetDataReaderAsync(sqlkv.Key, sqlkv.Value);
                await MiniExcel.SaveAsAsync(path, dataReader, overwriteFile: true);
                dataReader.Close();

                Growl.Success("导出成功。");
            }
            catch (Exception ex)
            {
                Growl.Error("导出失败：" + ex.Message);
            }
            finally
            {
                IsLoad1 = false;
            }

        }

        public ICommand UpdateListCommand { get => new DelegateCommand(UpdateList); }
        /// <summary>
        /// 更新信息
        /// </summary>
        public async void UpdateList()
        {
            if (IsLoad1)
            {
                Growl.Info("有任务正在进行，请稍等片刻。");
                return;
            }

            try
            {
                await Task.Delay(200);

                //1.查询数据库，加入筛选条件，并按照时间倒序排序
                var dbData = DataDb.db.Queryable<MyTestTable>()
                .WhereIF(TimeGo != null, o => o.Time > TimeGo)
                .WhereIF(TimeTo != null, o => o.Time < TimeTo)
                .WhereIF(!string.IsNullOrWhiteSpace(Info1), o => o.Info1.StartsWith(Info1.Trim()))
                .WhereIF(!string.IsNullOrWhiteSpace(Info2), o => o.Info2.StartsWith(Info2.Trim()))
                .OrderByDescending(o => o.Id);

                //2.开始分页（如模型不一样使用‘.Select<Model>()’来转换）
                RefAsync<int> totalNumber = 0;
                RefAsync<int> totalPage = 0;
                DataList = await dbData.ToPageListAsync(PageIndex, DataCountPerPage, totalNumber, totalPage);
                MaxPageCount = totalPage.Value;
            }
            catch (Exception ex)
            {
                Growl.Error("加载数据失败：" + ex.Message);
            }
            finally
            {
                IsLoad2 = false;
            }
        }

        public ICommand SeeCommand { get => new DelegateCommand<MyTestTable>(See); }
        /// <summary>
        /// 查看详情
        /// </summary>
        public void See(MyTestTable obj)
        {
            if (IsLoad1 || IsLoad2)
            {
                Growl.Info("有任务正在进行，请稍等片刻。");
                return;
            }

            Growl.Info("信息：" + Newtonsoft.Json.JsonConvert.SerializeObject(obj));
        }

    }
}
